home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8918 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  100 lines

  1. Path: s02.pavilion.co.uk!usenet
  2. From: AJRobb@pavilion.co.uk (Andy J Robb)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: revised code of calling a function twice from printf
  5. Date: Thu, 07 Mar 1996 06:25:46 GMT
  6. Organization: Pavilion Internet plc
  7. Message-ID: <4hlvfq$ja@s02.pavilion.co.uk>
  8. References: <4hfs54$k4e@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: poolc24.pavilion.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. razine@aol.com (Razine) wrote:
  13.  
  14. >Here is the actual code , modified with everyone's suggestions.  However
  15. >there is still a bug in here somewhere.  Can anyone please explain what is
  16. >wrong with this.
  17.  
  18. >It returns 
  19.  
  20. >[1] NONE       [2] NE
  21.  
  22. >I would like it to return
  23.  
  24. >[1] NONE       [2] Asprin
  25.  
  26. >Thank you very much for your help.  it is greatly appreciated.
  27.  
  28.  
  29. >#include <stdio.h>
  30. >#include <string.h>
  31.  
  32. >char *display_drug_type(int drug_index);
  33.  
  34. >int drug_inventory[5]={0,1,0,0,0};
  35.  
  36. >int main() {
  37.  
  38. >    printf("[1] %s             [2] %s  
  39. >\n",display_drug_type(0),display_drug_type(1));
  40.  
  41. The problem is that you are returning pointers to strings that were on
  42. the stack.  These strings will get re-used by the stack at some point.
  43. Two things you must do to make it work:
  44.  
  45. 1) declare char drug_type[81] as static (below).
  46. 2) do not call the function twice in a single printf() call.
  47.  
  48. printf("[1] %s     ", display_drug_type(0));
  49. printf("   [2] %s\n", display_drug_type(1));
  50.  
  51. >return 0;
  52. >        }
  53.  
  54. >char *display_drug_type(int drug_index) {
  55. >   char drug_type[81]="\0";
  56.  
  57. >   switch(drug_inventory[drug_index]) {
  58. >     case 0 : strcpy(drug_type,"NONE"); break;
  59. >     case 1 : strcpy(drug_type,"Asprin"); break;
  60. >     default : printf("Error in Display_drug_inventory");
  61. >                        }
  62. >   return drug_type;
  63. >}
  64.  
  65. Alternatively, a probably much safer (as well as faster, smaller etc.)
  66. routine that can be called as many times as you like in a printf():
  67.  
  68. const char *display_drug_type(int drug_index) 
  69. { switch(drug_index)
  70.   { case 0: 
  71.     case 2: 
  72.     case 3: 
  73.     case 4: return "NONE";
  74.     case 1: return "Asprin";
  75.    }
  76.  
  77.    frintf(stderr, "Error in Display_drug_inventory\n");
  78.    return "";
  79. }
  80.  
  81. Note: I removed the drug_inventory[] as this could lead to general
  82. protection faults (crashes) for bad drug_index.
  83.  
  84. Note: you should either place this code before main() or provide a
  85. function prototype before main().
  86.  
  87. Regards,
  88. Andy Robb.
  89. -----BEGIN PGP PUBLIC KEY BLOCK-----
  90. Version: 2.6.2i
  91.  
  92. mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
  93. MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
  94. B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
  95. tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
  96. IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
  97. =/wVD
  98. -----END PGP PUBLIC KEY BLOCK-----
  99.  
  100.